package com.ebelter.btcomlib.models.upload;

import android.os.Message;


import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.common.IOrder;
import com.ebelter.btcomlib.models.bluetooth.products.bpm.bean.BpmMesureResult;
import com.ebelter.btcomlib.models.bluetooth.products.bpm.bean.BpmMesureResult_Table;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.raizlabs.android.dbflow.sql.language.Select;

/**
 * 上传数据工具
 */

public class UploadDataUtils extends BaseHandle{

    private static final String TAG = "UploadDataUtils";


    private static final int WHAT_CHECK_DB_DATA = 1;
    public static final int UPLOAD_DATA_SUCCESS = 2;


    private boolean isUploadingFlag = false;//是否正在上传的标志

    private IOrder mIOrder;

    public UploadDataUtils(IOrder mIOrder, Object... objects) {
        super(objects);
        this.mIOrder = mIOrder;
    }

    @Override
    public void handleMsg(Message msg) {
        if (msg.what==WHAT_CHECK_DB_DATA){

            checkDbData();

            sendEmptyMessageDelayed(WHAT_CHECK_DB_DATA,10000);
        }
    }

    //j
    private void checkDbData() {
        long userID=UserSpUtil.readLong(Constants.IUser.USER_ID);
        if (userID<=0){
            LogUtils.i(TAG,"---checkDbData---userID小于等于零 " + userID);
            return;
        }
        BpmMesureResult bpmMesureResult = new Select().from(BpmMesureResult.class).where(BpmMesureResult_Table.userID.eq(userID)).querySingle();
        if (bpmMesureResult!=null){
            LogUtils.i(TAG,"---数据库检测到未上传的数据，准备上传");
            uploadBloodPressureDate(bpmMesureResult,false);
        }else {
            LogUtils.i(TAG,"---数据库未检测到缓存数据----");
        }



    }

    public void startCheckDB(){
        removeMessages(WHAT_CHECK_DB_DATA);
        sendEmptyMessage(WHAT_CHECK_DB_DATA);
    }

    public void exit(){
        removeMessages(WHAT_CHECK_DB_DATA);
    }

    public void uploadBloodPressureDate(final BpmMesureResult mesureResult, final boolean isNewData){

        if (mesureResult==null)return;






        if (mesureResult.userID<=0){
            LogUtils.i(TAG,"上传数据时因为用户id不正确,上传失败 userID="+mesureResult.userID);
            return;
        }


        if (!isNewData&&isUploadingFlag){
            LogUtils.i(TAG,"-- !isNewData && isUploadingFlag --为--true--暂时拒绝上传");
            return;
        }


        if (!NetUtils.available()){
            //网络不可用 先保存在数据库
            if (isNewData){
                LogUtils.i(TAG,"网络不可用,新的测量数据，保存在数据库");
                mesureResult.save();
            }
            return;
        }

        isUploadingFlag = true;

        //网络可以用就直接上传给后台
        NetUtils.getInstance().uploadBloodPressureDate(this, mesureResult.systolicPressure, mesureResult.diastolicPressure,
                mesureResult.pulse, mesureResult.testTime, new HttpResponse<BaseResponse>() {

            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                isUploadingFlag = false;
                if (!isRequestSuccess){
                    //上传失败的意思
                    if (isNewData){
                        mesureResult.save();
                    }
                    LogUtils.i(TAG,"-----上传失败1----"+mesureResult.toString());
                    return;
                }

                if (baseResponse.resultCode==0){

                    LogUtils.i(TAG,"-----上传成功----"+mesureResult.toString());
                    //表示上传成功
                    if (!isNewData){
                        mesureResult.delete();
                    }

                    if (mIOrder!=null)mIOrder.cmd(UPLOAD_DATA_SUCCESS,UPLOAD_DATA_SUCCESS);

                }else {
                    //表示上传失败
                    if (isNewData){
                        mesureResult.save();
                    }
                    LogUtils.i(TAG,"-----上传失败2----"+mesureResult.toString());
                }

            }

        });


    }
}
